﻿using System.ComponentModel;
using System.Device.Location;
using System.Runtime.Serialization;
using Coding4Fun.Controls.Data;
using Microsoft.Phone.Controls.Maps.Core;

namespace Coding4Fun.WheresMyCar
{
	[DataContract]
	public class WheresTheCarSettings : INotifyPropertyChanged
	{
		public static WheresTheCarSettings Instance { get; set; }

		static WheresTheCarSettings()
		{
			Instance = Load();
		}

		#region MeasurementMode (INotifyPropertyChanged Property)
		private MeasurementMode _measurementMode;
		[DataMember]
		public MeasurementMode MeasurementMode
		{
			get { return _measurementMode; }
			set
			{
				_measurementMode = value;
				RaisePropertyChanged("MeasurementMode");
			}
		}
		#endregion

		#region PersonalNote (INotifyPropertyChanged Property)
		private string _personalNote;
		[DataMember]
		public string PersonalNote
		{
			get { return _personalNote; }
			set
			{
				_personalNote = value;
				RaisePropertyChanged("PersonalNote");
			}
		}
		#endregion

		#region ZoomLevel (INotifyPropertyChanged Property)
		private int _zoomLevel;
		[DataMember]
		public int ZoomLevel
		{
			get { return _zoomLevel; }
			set
			{
				_zoomLevel = value;
				RaisePropertyChanged("ZoomLevel");
			}
		}
		#endregion

        #region UseAerialMode (INotifyPropertyChanged Property)
        private bool _useAerialMode;

        [DataMember]
        public bool UseAerialMode
        {
            get { return _useAerialMode; }
            set
            {
                _useAerialMode = value;
                RaisePropertyChanged("UseAerialMode");
            }
        }
        #endregion
        
		#region ParkingSpot (INotifyPropertyChanged Property)
		private GeoCoordinate _parkingSpot;
		[DataMember]
		public GeoCoordinate ParkingSpot
		{
			get { return _parkingSpot; }
			set
			{
				_parkingSpot = value;
				RaisePropertyChanged("ParkingSpot");
			}
		}
		#endregion

		#region Photo (INotifyPropertyChanged Property)
		private string _photo;
		[DataMember]
		public string Photo
		{
			get { return _photo; }
			set
			{
				_photo = value;
				RaisePropertyChanged("Photo");
			}
		}
		#endregion

        #region DistanceToCar (INotifyPropertyChanged Property)
        private double _distanceToCar;

		public double DistanceToCar
		{
			get { return _distanceToCar; }
			set
			{
				_distanceToCar = value;
                RaisePropertyChanged("DistanceToCar");
			}
		}
		#endregion

        #region HeadingToCar (INotifyPropertyChanged Property)
        private double _headingToCar;

        public double HeadingToCar
        {
            get { return _headingToCar; }
            set
            {
                _headingToCar = value;
                RaisePropertyChanged("HeadingToCar");
            }
        }
        #endregion

		public static WheresTheCarSettings Load()
		{
            WheresTheCarSettings t = StorageHelper.LoadContract<WheresTheCarSettings>("prefs.dat", true);
            if (Utility.IsCoordNullOrUnknown(t.ParkingSpot)) t.ParkingSpot = null; // Fix for XAML layout
		    return t;
		}

		public void Refresh()
		{
			var instance = Load();

			MeasurementMode = instance.MeasurementMode;
			PersonalNote = instance.PersonalNote;
			ZoomLevel = instance.ZoomLevel;
			ParkingSpot = instance.ParkingSpot;
			Photo = instance.Photo;
		}

		public void Save()
		{
			StorageHelper.SaveContract("prefs.dat", this, true);
		}

		#region INotifyPropertyChanged Members

		public event PropertyChangedEventHandler PropertyChanged;

		protected void RaisePropertyChanged(string propertyName)
		{
			if (PropertyChanged != null)
			{
				PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
			}
		}
		#endregion
	}
}